﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Script.Serialization;

namespace Omu.Awesome.Mvc.Helpers
{
    public static class PopupFormHelpers
    {
        public static MvcHtmlString GetPopupFormId(this UrlHelper url, string action, string controller = null)
        {
            return MvcHtmlString.Create("pf" + (action + (controller ?? url.Controller())).ToLower());
        }
        public static IHtmlString PopupFormActionLink<T>(this HtmlHelper html, Expression<Action<T>> e, string text = null, object htmlAttributes = null)
            where T : Controller
        {
            return
                MvcHtmlString.Create(
                    string.Format("<a href='javascript:{0}' {2} >{1}</a>",
                    PopupFormAction(new UrlHelper(html.ViewContext.RequestContext), e),
                                  text ?? e.Name(), htmlAttributes.GetDictionary().ToHtml()));
        }

        public static MvcHtmlString PopupFormAction<T>(this UrlHelper url, Expression<Action<T>> e) where T : Controller
        {
            return PopupFormAction(url, e.Name(), e.GetValues(), typeof(T).Controller());
        }

        public static MvcHtmlString PopupFormActionLink(this HtmlHelper html, string action, string text = null, IEnumerable<object> parameters = null, object htmlAttributes = null, string controller = null)
        {
            return
                MvcHtmlString.Create(
                    string.Format("<a href='javascript:{0}' {2} >{1}</a>", PopupFormAction(new UrlHelper(html.ViewContext.RequestContext), action, parameters, controller),
                                  text ?? action, htmlAttributes.GetDictionary().ToHtml()));
        }

        public static MvcHtmlString PopupFormAction(this UrlHelper url, string action, IEnumerable<object> parameters = null, string controller = null)
        {
            var funcParams = parameters != null ? parameters
                .Aggregate("", (current, o) => current + new JavaScriptSerializer().Serialize(o) + ",")
                .RemoveSuffix(",") : string.Empty;

            return MvcHtmlString.Create("callpf" + (action + (controller ?? url.Controller())).ToLower() + "(" + funcParams + ")");
        }

        public static MvcHtmlString MakePopupForm(this HtmlHelper html,
            string action,
            string[] parameters = null,
            string controller = null,
            string title = null,
            int? height = null,
            int? width = null,
            bool? refreshOnSuccess = null,
            string okText = null,
            string cancelText = null,
            bool? clientSideValidation = null,
            string successFunction = null,
            bool? modal = null,
            bool? resizable = null,
            string position = null,
            bool? fullScreen = null)
        {
            if (Settings.GetText != null)
            {
                if (okText == null) okText = Settings.GetText("PopupForm", "Ok");
                if (cancelText == null) cancelText = Settings.GetText("PopupForm", "Cancel");
                if (title == null) title = Settings.GetText("PopupForm", "Title");
            }
            return html.Partial(Settings.AwesomeFolder + "PopupForm.ascx",
                new PopupFormInfo
                {
                    Modal = modal ?? Settings.PopupForm.Modal,
                    Resizable = resizable ?? Settings.PopupForm.Resizable,
                    Action = action,
                    Controller = controller ?? html.Controller(),
                    Title = title,
                    Height = height ?? Settings.PopupForm.Height,
                    Width = width ?? Settings.PopupForm.Width,
                    Parameters = parameters ?? new string[] { },
                    RefreshOnSuccess = refreshOnSuccess ?? Settings.PopupForm.RefreshOnSuccess,
                    OkText = okText ?? Settings.PopupForm.OkText,
                    CancelText = cancelText ?? Settings.PopupForm.CancelText,
                    ClientSideValidation = clientSideValidation ?? Settings.PopupForm.ClientSideValidation,
                    SuccessFunction = successFunction,
                    Position = position.JQPos(),
                    FullScreen = fullScreen ?? false
                });
        }

        public static MvcHtmlString MakePopupForm<T>(
            this HtmlHelper html,
            Expression<Action<T>> e,
            string title = null,
            int? height = null,
            int? width = null,
            bool? refreshOnSuccess = null,
            string okText = null,
            string cancelText = null,
            bool? clientSideValidation = null,
            string successFunction = null,
            bool? modal = null,
            bool? resizable = null,
            string position = null,
            bool? fullScreen = null
            ) where T : Controller
        {
            return MakePopupForm(html, e.Name(), e.Params(), typeof(T).Controller(), title, height, width, refreshOnSuccess, okText,
                                 cancelText, clientSideValidation, successFunction, modal, resizable, position, fullScreen);
        }
    }
}